<?php

namespace app\platform\business;

use app\platform\model\MerchantRoleRelation as MerchantRoleRelationModel;
use app\platform\model\RoleMenuRelation as RoleMenuRelationModel;
use app\platform\model\Role as RoleModel;

class Role extends PlatformBase
{
    private $_roleObj = null;

    public function __construct()
    {
        $this->_roleObj = new RoleModel();
    }

    public function getAllRole(array $where = []): array
    {
        $rows = null;
        try {
            $rows = $this->_roleObj->getAllRole($where);
        } catch (\Exception $e) {
            logErr('读取所有角色失败', sprintf('method:%s,err:%s', __METHOD__, $e->getMessage()));
        }

        return $rows ? $rows->toArray() : [];
    }

    public function getRoleList(int $page = 1, int $limit = 10, array $where = []): array
    {
        $rows  = [];
        $count = 0;
        try {
            $rows  = $this->_roleObj->getRoleList($page, $limit, $where);
            $count = $this->_roleObj->getRoleCount($where);
        } catch (\Exception $e) {
            logErr('读取角色列表失败', sprintf('method:%s,err:%s', __METHOD__, $e->getMessage()));
        }

        return ['rows' => $rows ? $rows->toArray() : [], 'count' => $count];
    }

    public function addRole(array $data): array
    {
        try {
            return [self::_RESULT => $this->_roleObj->addRole($data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('添加角色失败', sprintf('method:%s,err:%s', __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function findRoleById(int $id): array
    {
        $role = null;
        try {
            $role = $this->_roleObj->findRoleById($id);
        } catch (\Exception $e) {
            logErr('通过主键读取角色详细信息失败', sprintf('method:%s,err:%s', __METHOD__, $e->getMessage()));
        }

        if (empty($role)) {
            return [];
        }

        return $role->toArray();
    }

    public function updateRole(int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_roleObj->updateRole($id, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新角色信息失败', sprintf('method:%s,err:%s', __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteRole(int $id): array
    {
        try {
            return [self::_RESULT => $this->_roleObj->deleteRole($id), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键删除角色失败', sprintf('method:%s,err:%s', __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }


    public function getAllRoleMenuRelation(int $merchantId): array
    {
        try {
            $roleIds                 = [];
            $merchantRoleRelationObj = new MerchantRoleRelationModel();
            $merchantRoleRelations   = $merchantRoleRelationObj->getAllMerchantRoleRelation(['merchant_id' => $merchantId]);

            foreach ($merchantRoleRelations as $merchantRoleRelation) {
                $roleIds [] = $merchantRoleRelation['role_id'];
            }

            $RoleMenuObj = new RoleMenuRelationModel();
            return $RoleMenuObj->getAllRoleMenuRelation([['role_id', 'in', $roleIds]])->toArray();
        } catch (\Exception $e) {
            logErr('通过商户id读取商户的角色关联关系失败', sprintf('method:%s,err:%s', __METHOD__, $e->getMessage()));
        }

        return [];
    }

    public function getAllRoleMenuRelationByRoleId(int $roleId): array
    {
        try {
            $RoleMenuObj = new RoleMenuRelationModel();
            return $RoleMenuObj->getAllRoleMenuRelation([['role_id', 'in', $roleId]])->toArray();
        } catch (\Exception $e) {
            logErr('通过角色id读取商户的角色关联关系失败', sprintf('method:%s,err:%s', __METHOD__, $e->getMessage()));
        }

        return [];
    }

    public function updateRoleMenuRelation(int $roleId, array $rows): array
    {
        try {
            $RoleMenuObj = new RoleMenuRelationModel();
            return [self::_RESULT => $RoleMenuObj->updateRoleMenuRelation($roleId, $rows), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过角色id更新商户的角色关联关系失败', sprintf('method:%s,err:%s', __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }
}
